﻿using NLog.Config;
using NLog.Targets;
using NLog;
using System;
using System.Collections.Generic;

namespace OkTool.Log
{
    public class NlogUtil
    {
        private static bool init=false;
       
        public static void Init()
        {
            // Step 1. Create configuration object 
            var config = new LoggingConfiguration();

            var fileTarget1 = new FileTarget();
            config.AddTarget("file", fileTarget1);
            fileTarget1.FileName = "${basedir}/Logs/${shortdate}/${shortdate}.txt";
            fileTarget1.Layout = "${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}";
            fileTarget1.ArchiveFileName = "${basedir}/Logs/${shortdate}/${shortdate}-{#####}.txt";
            fileTarget1.ArchiveAboveSize = 10485760;//10M
            fileTarget1.ArchiveNumbering = ArchiveNumberingMode.Sequence;


            config.LoggingRules.Add(new LoggingRule(NlogLogger.Log.ToString(), LogLevel.Debug, fileTarget1));

            LogManager.Configuration = config;

        }
        public enum NlogLogger
        {
            Log,
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="level"></param>
        public static void Log(string message,LogLevel level=null)
        {
            if (level == null) { level = LogLevel.Debug; }
            NLog.LogManager.GetLogger(NlogLogger.Log.ToString()).Log(level, message);
        }
    }
}
